package cn.chengpengper.transfer.listener;

import cn.chengpengper.transfer.provider.TranslatorTextProvider;
import cn.chengpengper.transfer.ui.TranslatorWindow;
import cn.chengpengper.transfer.util.TranslatorUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;

public class TranslatorButtonActionListener extends AbstractAction {

    private final TranslatorWindow window;

    private final Map<String, String> langMap;

    public TranslatorButtonActionListener(TranslatorWindow window) {
        this.window = window;
        langMap = new HashMap<>(2);
        langMap.put("中文", "zh");
        langMap.put("英文", "en");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String originalText = window.getOriginalTextArea().getText();
        String from = langMap.get((String) window.getComboBox1().getSelectedItem());
        String to = langMap.get((String) window.getComboBox2().getSelectedItem());

        String result = TranslatorUtils.getTransResult(originalText, from, to);
        window.getTranslateTextArea().setText(result);

        // 点击翻译按钮时，将原文与译文添加到自动提示 provider 的提示列表中
        TranslatorTextProvider.items.add(originalText);
        TranslatorTextProvider.items.add(result);
    }
}
